home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0997.zip / jdead.txt < prev    next >
Text File  |  1997-07-22  |  3KB  |  129 lines

  1. _Java Deadlock_
  2. by Allan Vermeulen
  3.  
  4. Listing One
  5. public class Resource implements Runnable {
  6.   private Resource boss_;   // a property
  7.   public void setBoss(Resource boss)  {boss_=boss;}
  8.   public Resource getBoss() {return boss_;}
  9.  
  10.   public synchronized void assignProject() {pause();}
  11.   public synchronized void receiveProject() {}
  12.   public synchronized void run() {
  13.     pause();
  14.     getBoss().assignProject();
  15.   }
  16.   public static void pause() {
  17.     try { Thread.sleep(700); }
  18.     catch(InterruptedException e) { }
  19.   }
  20. }
  21.  
  22. Listing Two
  23. public class Launch {
  24.   public static void main(String argv[]) {
  25.     Resource boss    = new Resource();
  26.     Resource dilbert = new Resource();
  27.     Resource wally   = new Resource();
  28.     dilbert.setBoss(boss);
  29.     wally.setBoss(boss);
  30.  
  31.     // Build project threads
  32.     Thread dilbertThread = new Thread(dilbert);
  33.     Thread wallyThread = new Thread(wally);
  34.  
  35.     // Run the project threads
  36.     dilbertThread.start();
  37.     wallyThread.start();
  38.   }
  39. }
  40.  
  41. Listing Three
  42. public class Dead1 {
  43.   public static void main(String argv[]) {
  44.     // Create an organization that gets no work done
  45.     Resource dilbert = new Resource();
  46.     Resource wally   = new Resource();
  47.     dilbert.setBoss(wally);
  48.     wally.setBoss(dilbert);
  49.  
  50.     // Build project threads
  51.     Thread dilbertThread = new Thread(dilbert);
  52.     Thread wallyThread = new Thread(wally);
  53.  
  54.     // Run the project threads
  55.     dilbertThread.start();
  56.     wallyThread.start();
  57.   }
  58. }
  59.  
  60. Listing Four
  61. public class Manager extends Resource {
  62.   private boolean hasProject_ = false;
  63.   public synchronized void receiveProject() {
  64.     hasProject_ = true;
  65.     notify();
  66.   }
  67.   public synchronized void assignProject() {
  68.     while (hasProject_ == false) {
  69.       try { wait(); }
  70.       catch(InterruptedException e) { }
  71.     }
  72.     hasProject_ = false;
  73.   }
  74. }
  75.  
  76. Listing Five
  77. public class Signal {
  78.   public static void main(String argv[]) {
  79.     // Create an organization to get work done
  80.     Resource boss    = new Manager();
  81.     Resource dilbert = new Resource();
  82.     Resource catbert = new Marketer();
  83.     dilbert.setBoss(boss);
  84.     catbert.setBoss(boss);
  85.  
  86.     // Build the threads in which projects will be done
  87.     Thread dilbertThread = new Thread(dilbert);
  88.     Thread catbertThread = new Thread(catbert);
  89.  
  90.     // Run the project threads
  91.     dilbertThread.start();
  92.     Resource.pause(); // Dilbert starts first
  93.     catbertThread.start();
  94.   }
  95. }
  96.  
  97. Listing Six
  98. public class WimpManager extends Resource {
  99.   public synchronized void receiveProject()
  100.     { getBoss().receiveProject(); }
  101.   public synchronized void assignProject()
  102.     { getBoss().assignProject(); }
  103. }
  104.  
  105. Listing Seven
  106. public class Dead2 {
  107.   public static void main(String argv[]) {
  108.     // Create an organization to get work done
  109.     Resource wimp    = new WimpManager();
  110.     Resource boss =    new Manager();
  111.     Resource dilbert = new Resource();
  112.     Resource catbert = new Marketer();
  113.     wimp.setBoss(boss);
  114.     dilbert.setBoss(wimp);
  115.  
  116.     // Build the threads in which projects will be done
  117.     Thread dilbertThread = new Thread(dilbert);
  118.     Thread catbertThread = new Thread(catbert);
  119.  
  120.     // Run the project threads
  121.     dilbertThread.start();
  122.     Resource.pause();  // Dilbert asks first
  123.     catbertThread.start();
  124.   }
  125. }
  126.  
  127.  
  128.  
  129.